//
//  Preset.swift
//  EZAMP
//
//  Created by 谭圣 on 2021/1/6.
//  Copyright © 2021 ts. All rights reserved.
//

import 'package:flutter_soundmagus/widget/eq/eq.dart';

/// 数据
class Data {
  List<List<double>> eq;
  List<List<int>> xover;

  Data({required this.eq, required this.xover});
}

/// 预设
abstract class Preset {
  ///31段eq
  static List<List<double>> _presetsEQ31 = [
    [-1,-1,-1,-1,-1,-1,0,0,0,0,1,2,3,4,5,6,5,4,3,2,2,2,1,0,0,0,0,-1,-1,-1,-1],
    [8,8,8,7,6,5,4,3,2,1,0,0,1,2,3,3,3,4,5,6,7,6,5,4,3,2,1,0,0,0,0],
    [5,6,7,5,3,1,3,4,4,6,4,2,0,-1,-3,0,1,2,3,4,5,6,7,5,4,6,7,6,5,4,4],
    [6,5,5,4,3,2,1,1,0,0,0,1,1,0,-1,0,0,0,0,1,2,3,4,3,3,3,3,3,3,3,3],
    [-4,-4,-4,-4,-4,-4,-3,-2,-2,-2,-1,2,3,4,4,6,7,9,9,7,5,4,2,1,0,-1,-2,-2,-3,-3,-3],
    [7,6,5,4,5,6,7,8,9,9,8,6,4,2,0,0,-1,-2,-3,-4,-2,-1,0,1,2,3,2,1,0,-1,-2]
  ];
  static List<Data> _presets = [
    //HD 2014-2018
    Data(eq: [
      [30, 60, 90, 750, 1250, 3200, 6000, 16000],
      [-20, 12, -20, 0, 3.5, 3.0, -3.0, -3.5],
      [1, 1.8, 0.5, 0.05, 2, 1, 3.5, 2]
    ], xover: [
      [0, 1, 80],
      [0, 1, 70]
    ]),
    //HD14-18 HORNS
    Data(eq: [
      [30, 60, 90, 750, 1250, 3200, 8000, 16000],
      [-20, 12, -20, 0, 3.5, -3.0, -6.0, -3.5],
      [1, 1.8, 0.5, 0.05, 2, 1, 3.5, 2]
    ], xover: [
      [0, 1, 80],
      [0, 1, 70]
    ]),
    //HD19 NEWER
    Data(eq: [
      [80, 160, 500, 750, 2500, 3200, 6300, 10000],
      [-20, 6, -8.5, 0, -7.5, 0, -3.2, -11.2],
      [0.5, 1.8, 0.5, 0.05, 4.3, 0.05, 4.3, 1.6]
    ], xover: [
      [0, 1, 90],
      [0, 1, 60]
    ]),
    //HD19 NEWER-HORNS
    Data(eq: [
      [60, 80, 160, 500, 2500, 3200, 6300, 10000],
      [6, -20, 6, -8.5, -7.5, 0, -9.2, -11.2],
      [6, 0.5, 1.8, 2, 4.3, 0.05, 4.3, 1.6]
    ], xover: [
      [0, 1, 90],
      [0, 1, 60]
    ]),
    Data(eq: [
      [50, 125, 200, 750, 2000, 5000, 8000, 16000],
      [6, 0, -3, 0, -3, 3, -4, -2],
      [5, 0.05, 5, 0.05, 2, 4, 5, 7]
    ], xover: [
      [0, 1, 90],
      [0, 1, 60]
    ]),
    //@[@50, @125, @315, @750, @1250, @3200, @6000, @16000]
    Data(eq: [
      [50, 100, 315, 750, 2000, 6000, 16000, 20000],
      [6, -2, 0, 0, -6, -9, -9, 0],
      [1, 4, 0.05, 0.05, 2, 5, 10, 0.05]
    ], xover: [
      [0, 1, 90],
      [0, 1, 60]
    ]),
  ];

  ///选中预设
  static void set(int index) {
    if (index >= _presets.length) {
      return;
    }
    var data = _presets[index], eq = data.eq, xover = data.xover;

    //eq
    final pdCount = DataMng().eqModel.items.first.length;
    for (var selectCH = 0; selectCH < kChCount; selectCH++) {
      for (var i = 0; i < pdCount; i++) {
        final itemModel = DataMng().eqModel.items[selectCH][i];
        itemModel.freq = kPdCount == 8 ? eq.first[i].toInt() : EQModel.freqs[i];

        itemModel.ch = kPdCount == 8 ? eq[1][i] : _presetsEQ31[index][i];
        itemModel.q = i < eq.last.length ? eq.last[i] : kQDefault;
      }
    }
    //xover
    for (var i = 0; i < 4; i++) {
      if (i < 2) {
        final high = DataMng().xoverModel.items[i].high;
        high.type = xover.first[0];
        high.slope = xover.first[1];
        high.freq = xover.first[2];
      } else {
        final high = DataMng().xoverModel.items[i].high;
        high.type = xover.last[0];
        high.slope = xover.last[1];
        high.freq = xover.last[2];
      }
    }

    //音量
    for (var i = 0; i < kChCount; i++) {
      final item = DataMng().advancedModel.level.items[i];
      item.volume = 0;
    }
    DataMng().advancedModel.level.totalModel.volume = 0;
    MsgParser.done(sendData: true);
  }
}
